﻿using Beryl.Util;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Beryl.Model
{
    //安卓系统相关
    public class SystemController
    {
        private static SystemController instance = null;
        private static readonly object uniqueLock = new object();

        public static SystemController Instance
        {
            get
            {
                lock (uniqueLock)
                {
                    if (instance == null)
                    {
                        instance = new SystemController();
                    }
                }
                return instance;
            }
        }
        public SystemController() { }


        public List<string> getVersion()
        {
            string name = System.Reflection.MethodBase.GetCurrentMethod().Name;
            List<string> strings = new List<string>();
            try
            {
                string sql = "select * from [dbo].[AndroidVersion]";
                string result = JsonConvert.SerializeObject(CommonMethod.Instance.selectDatabase(sql));
                if(result.Equals("[]"))
                {
                    strings.Add("获取版本号失败");
                }else
                {
                    strings.Add(result);
                }
                //CommonMethod.Instance.InsertLog(name, strings[0], CommonMethod.LogType.Info, "","获取版本信息");
                CommonMethod.Instance.InsertLog1("", strings[0], "", CommonMethod.LogType.Info, name, "", true);
            }
            catch(Exception e)
            {
                strings.Add(e.Message);
                //CommonMethod.Instance.InsertLog(name, e.Message, CommonMethod.LogType.Info, "","获取版本信息异常");
                CommonMethod.Instance.InsertLog1("", e.Message, "", CommonMethod.LogType.Error, name, "", true);
                return strings;
            }
            return strings;
        }


        public List<string> selectAllCargoInfor(string User)
        {
            List<string> list = new List<string>();
            string name = System.Reflection.MethodBase.GetCurrentMethod().Name;
            try
            {
                string sql = "select * from SYS_User where LoginUser = '" + User + "'";
                string result = JsonConvert.SerializeObject(CommonMethod.Instance.selectDatabase(sql));
                if (result.Equals("[]"))
                {
                    list.Add("failed:找不到账号!请检查账号是否正确。");
                }else
                {
                    list.Add(result);
                }
            }
            catch (Exception e)
            {
                list.Add("failed:" + e.Message.ToString());
                // CommonMethod.Instance.InsertLog(name, e.Message, CommonMethod.LogType.Info, "",User);
                CommonMethod.Instance.InsertLog1("", User + ":"+ e.Message, "", CommonMethod.LogType.Error, name, "", true);
            }
            //CommonMethod.Instance.InsertLog(name, list[0], CommonMethod.LogType.Info, "",User);
            CommonMethod.Instance.InsertLog1("", list[0], "", CommonMethod.LogType.Info, name, "", true);
            return list;
        }

        public string selectgetPr(string Pr,string Data)
        {
            string name = System.Reflection.MethodBase.GetCurrentMethod().Name;
            string Pr1;
            try
            {
                string sql = string.Format(@"select * from Sys_PsnNum where cPsn_Num like '%{0}' or cPsn_Num = '{0}'", Pr);
                Pr1 = JsonConvert.SerializeObject(CommonMethod.Instance.selectDatabase(sql));//转为json

                if (Pr1.Equals("[]"))
                {
                    return "扫描的工号错误";
                }
                Pr1 = Pr1.Remove(0, 1);
                Pr1 = Pr1.Remove(Pr1.Length - 1, 1);
            }
            catch (Exception e)
            {
                //CommonMethod.Instance.InsertLog(name, e.Message, CommonMethod.LogType.Error, Data,Pr);
                CommonMethod.Instance.InsertLog1("", Pr + ":" + e.Message, "", CommonMethod.LogType.Error, name, "", true);
                return e.Message;
            }
            //CommonMethod.Instance.InsertLog(name, Pr1, CommonMethod.LogType.Info, Data, Pr);
            CommonMethod.Instance.InsertLog1("", Pr1, Data, CommonMethod.LogType.Info, name, Pr, true);
            return Pr1;
        }

        public string selectgetEQ(string para, string Data)
        {
            string name = System.Reflection.MethodBase.GetCurrentMethod().Name;
            string eq1;
            Newtonsoft.Json.Linq.JArray jsonArr = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(para);
            var k = jsonArr[0];
            try
            {
                
                string sql = "";
                if (!k["设备"].ToString().Contains("EQ-"))
                {
                    sql = string.Format(@"select cDeviceName,cSuperviseNo from EM_EquipmentLedger where cDepartment = '{0}' and cSuperviseNo = '{1}'", k["参数"].ToString(), k["设备"].ToString());
                }else
                {
                    sql = string.Format(@"select cDeviceName,cSuperviseNo from EM_EquipmentLedger where cDepartment = '{0}' and barcode = '{1}'", k["参数"].ToString(), k["设备"].ToString());
                }
                //string currentProcessName = "裁切";
                 
                eq1 = JsonConvert.SerializeObject(CommonMethod.Instance.selectDatabase(sql));//转为json
                
                if (eq1.Equals("[]"))
                {
                    throw new Exception("设备维护或不存在");
                }else
                {
                    Newtonsoft.Json.Linq.JArray jsonArr1 = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(eq1);
                    if (jsonArr1.Count > 1)
                    {
                        throw new Exception("查询出多个设备,出错");
                    }
                    eq1 = eq1.Remove(0, 1);
                    eq1 = eq1.Remove(eq1.Length - 1, 1);
                }
            }
            catch (Exception e)
            {
                // CommonMethod.Instance.InsertLog(name, e.Message, CommonMethod.LogType.Error, Data,para);
                CommonMethod.Instance.InsertLog1(k["设备"].ToString(), e.Message, Data, CommonMethod.LogType.Error, name, "", true);
                return e.Message;
            }
            //CommonMethod.Instance.InsertLog(name, eq1, CommonMethod.LogType.Info, Data,para);
            CommonMethod.Instance.InsertLog1(k["设备"].ToString(), eq1, Data, CommonMethod.LogType.Info, name, "", true);
            return eq1;
        }


        public List<string> updateException(string json,string data)
        {
            string name = System.Reflection.MethodBase.GetCurrentMethod().Name;
            //CommonMethod.Instance.InsertLog(name, json, CommonMethod.LogType.Error, data, "异常记录插入");
            CommonMethod.Instance.InsertLog1("", json, data, CommonMethod.LogType.Error, name, "", true);
            List<string> re = new List<string>();
            re.Add("OK");
            return re;
        }
    }
}